#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Copyright (c) 2025 粤港澳大湾区（广东）国创中心

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

File: NctiLineEditBoundData.py
Desc: Custom QLineEdit, QComboBox, and QPushButton classes for bound data input.
Author: xumanjia
Date: 2025-08-01
"""
import sys
from PyQt5.QtWidgets import QApplication, QFrame, QHBoxLayout, QLabel, QLineEdit,QComboBox, QDialog, QMessageBox, QWidget, QVBoxLayout, QFormLayout, QPushButton
from PyQt5.QtGui import QFont, QColor, QIcon, QKeySequence, QRegExpValidator
from PyQt5.QtCore import QRegExp, Qt, QTimer, QThread, pyqtSignal, QProcess


Ncti_InterPattern = "^(-?\\d+)(\\d+)?$"
Ncti_FloatPattern = "^(-?\\d+)(\\.\\d+)?$"
class NctiLineEditBoundData(QLineEdit):
    def __init__(self, value, parent=None):
        super().__init__(parent)
        self.value = value
        self.setText(str(value))
        # self.setAlignment(Qt.AlignCenter)
        # self.setStyleSheet("QLineEdit { background-color: rgb(238, 242, 255); border-radius: 5px; }")
        # self.setFont(QFont("Arial", 12))
        # self.setFixedHeight(30)
    
        if isinstance(value, str):
            self.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        elif isinstance(value, int):
            regExpinter = QRegExp(Ncti_InterPattern)
            self.setValidator(QRegExpValidator(regExpinter, parent))
            self.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        elif isinstance(value, float):
            regExpinter = QRegExp(Ncti_FloatPattern)
            self.setValidator(QRegExpValidator(regExpinter, parent))
            self.setAlignment(Qt.AlignRight | Qt.AlignVCenter)

    def accept(self):
        if isinstance(self.value, str):
            self.value = self.text()
        elif isinstance(self.value, int):
            self.value = int(self.text())
        elif isinstance(self.value, float):
            self.value = float(self.text())
        
class NctiComboBoxData(QComboBox):
    def __init__(self, valueList, parent=None):
        super().__init__(parent)
        self.valueList = valueList
        for item in self.valueList:
            self.addItem(item)


    def accept(self):
        if isinstance(self.valueList,list):
            text = self.currentText()
            self.valueList.clear
            self.valueList.append(text)

class NctiPushButtonData(QPushButton):
    def __init__(self, text, value, showControl, parent = None):
        super().__init__(parent)
        self.setText(text)
        self.showControl = showControl
        if isinstance(value, dict) and "sel" in value:
            value['names'] = value.pop('sel')
        self.value = value

    def setShowText(self, text):
        self.showControl.setText(text)
        
    def setValue(self, selData):
        self.value = selData
    
    def getValue(self):
        return self.value

